package com.woniuxy.cloud.carl.order.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.carl.order.client.OrderQueryClient;
import com.woniuxy.cloud.carl.order.client.result.OrderItemDto;
import com.woniuxy.cloud.carl.order.client.result.OrderDetailDto;
import com.woniuxy.cloud.carl.order.infra.model.Order;
import com.woniuxy.cloud.carl.order.infra.model.OrderItem;
import com.woniuxy.cloud.carl.order.infra.model.ServiceOrderItem;
import com.woniuxy.cloud.carl.order.infra.model.SkuOrderItem;
import com.woniuxy.cloud.carl.order.service.OrderItemService;
import com.woniuxy.cloud.carl.order.service.OrderService;
import com.woniuxy.cloud.carl.order.service.ServiceOrderItemService;
import com.woniuxy.cloud.carl.order.service.SkuOrderItemService;
import com.woniuxy.cloud.carl.user.client.UserQueryClient;
import com.woniuxy.cloud.carl.user.client.result.UserDetailDto;
import com.woniuxy.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class OrderQueryApiImpl implements OrderQueryClient {

    @Resource
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private ServiceOrderItemService serviceOrderItemService;
    @Autowired
    private SkuOrderItemService skuOrderItemService;
    @Autowired
    private UserQueryClient userQueryClient;

    @Override
    public Result<List<OrderDetailDto>> getUserOrder(Long userId) {
        List<Order> orders = orderService.list(new QueryWrapper<Order>().eq("user_id", userId));
        List<OrderDetailDto> dtos = new ArrayList<>();
        for (Order order : orders) {
            UserDetailDto userDetailDto = userQueryClient.getUserDetail(order.getUserId()).getData();
            OrderDetailDto orderDetailDto = BeanUtil.toBean(order, OrderDetailDto.class);
            List<OrderItem> orderItems = orderItemService.list(new QueryWrapper<OrderItem>().eq("order_id", order.getOrderId()));
            List<OrderItemDto> orderItemDtos = new ArrayList<>();
            for (OrderItem orderItem : orderItems) {
                OrderItemDto orderItemDto = BeanUtil.toBean(orderItem, OrderItemDto.class);
                SkuOrderItem skuItem = skuOrderItemService.getOne(new QueryWrapper<SkuOrderItem>().eq("order_item_id", orderItem.getOrderItemId()));
                if(skuItem!=null) {
                    orderItemDto.setSkuId(skuItem.getSkuId());
                    orderItemDto.setSkuCount(skuItem.getSkuCount());
                    orderItemDto.setSkuName(skuItem.getSkuName());
                    orderItemDto.setSkuOrderItemId(skuItem.getSkuOrderItemId());
                    orderItemDto.setDeliveryStatus(skuItem.getDeliveryStatus());
                    orderItemDto.setUserId(order.getUserId());
                    orderDetailDto.setUserName(userDetailDto.getUserName());
                }
                ServiceOrderItem serviceOrderItem = serviceOrderItemService.getOne(new QueryWrapper<ServiceOrderItem>().eq("order_item_id", orderItem.getOrderItemId()));
                if(serviceOrderItem!=null){
                    orderItemDto.setServiceOrderItemId(serviceOrderItem.getServiceOrderItemId());
                    orderItemDto.setServiceId(serviceOrderItem.getServiceId());
                    orderItemDto.setStoreId(serviceOrderItem.getStoreId());
                    orderItemDto.setServiceName(serviceOrderItem.getServiceName());
                    orderItemDto.setServiceStatus(serviceOrderItem.getServiceStatus());
                    orderItemDto.setUserId(order.getUserId());
                    orderDetailDto.setUserName(userDetailDto.getUserName());
                }
                orderItemDtos.add(orderItemDto);
            }
            orderDetailDto.setOrderItemDtoList(orderItemDtos);
            if(orderDetailDto.getOrderStatus()==0){
                orderDetailDto.setOrderStatusName("待支付");
            }
            if(orderDetailDto.getOrderStatus()==1){
                orderDetailDto.setOrderStatusName("已支付");
            }
            if(orderDetailDto.getOrderStatus()==2){
                orderDetailDto.setOrderStatusName("已取消");
            }
            if(orderDetailDto.getOrderStatus()==3){
                orderDetailDto.setOrderStatusName("支付中");
            }
            if(orderDetailDto.getOrderStatus()==4){
                orderDetailDto.setOrderStatusName("已完成");
            }
            dtos.add(orderDetailDto);
        }
        return Result.success(dtos);
    }

    @Override
    public Result<OrderDetailDto> getOrderByNum(String orderNum) {
        Order order = orderService.getOne(new QueryWrapper<Order>().eq("order_num", orderNum));
        UserDetailDto userDetailDto = userQueryClient.getUserDetail(order.getUserId()).getData();
        OrderDetailDto orderDetailDto = BeanUtil.toBean(order, OrderDetailDto.class);
        List<OrderItem> orderItems = orderItemService.list(new QueryWrapper<OrderItem>().eq("order_id", order.getOrderId()));
        List<OrderItemDto> orderItemDtos = new ArrayList<>();
        for (OrderItem orderItem : orderItems) {
            OrderItemDto orderItemDto = BeanUtil.toBean(orderItem, OrderItemDto.class);
            SkuOrderItem skuItem = skuOrderItemService.getOne(new QueryWrapper<SkuOrderItem>().eq("order_item_id", orderItem.getOrderItemId()));
            if(skuItem!=null) {
                orderItemDto.setSkuId(skuItem.getSkuId());
                orderItemDto.setSkuCount(skuItem.getSkuCount());
                orderItemDto.setSkuName(skuItem.getSkuName());
                orderItemDto.setSkuOrderItemId(skuItem.getSkuOrderItemId());
                orderItemDto.setDeliveryStatus(skuItem.getDeliveryStatus());
                orderItemDto.setUserId(order.getUserId());
                orderDetailDto.setUserName(userDetailDto.getUserName());
            }
            ServiceOrderItem serviceOrderItem = serviceOrderItemService.getOne(new QueryWrapper<ServiceOrderItem>().eq("order_item_id", orderItem.getOrderItemId()));
            if(serviceOrderItem!=null){
                orderItemDto.setServiceOrderItemId(serviceOrderItem.getServiceOrderItemId());
                orderItemDto.setServiceId(serviceOrderItem.getServiceId());
                orderItemDto.setStoreId(serviceOrderItem.getStoreId());
                orderItemDto.setServiceName(serviceOrderItem.getServiceName());
                orderItemDto.setServiceStatus(serviceOrderItem.getServiceStatus());
                orderItemDto.setUserId(order.getUserId());
                orderDetailDto.setUserName(userDetailDto.getUserName());
            }
            orderItemDtos.add(orderItemDto);

        }
        orderDetailDto.setOrderItemDtoList(orderItemDtos);
        if(orderDetailDto.getOrderStatus()==0){
            orderDetailDto.setOrderStatusName("待支付");
        }
        if(orderDetailDto.getOrderStatus()==1){
            orderDetailDto.setOrderStatusName("已支付");
        }
        if(orderDetailDto.getOrderStatus()==2){
            orderDetailDto.setOrderStatusName("已取消");
        }
        if(orderDetailDto.getOrderStatus()==3){
            orderDetailDto.setOrderStatusName("支付中");
        }
        if(orderDetailDto.getOrderStatus()==4){
            orderDetailDto.setOrderStatusName("已完成");
        }
        return Result.success(orderDetailDto);
    }

    @Override
    public Result<OrderDetailDto> getOrderDetail(Long orderId) {
        Order order = orderService.getById(orderId);
        UserDetailDto userDetailDto = userQueryClient.getUserDetail(order.getUserId()).getData();
        OrderDetailDto orderDetailDto = BeanUtil.toBean(order, OrderDetailDto.class);
        List<OrderItem> orderItems = orderItemService.list(new QueryWrapper<OrderItem>().eq("order_id", order.getOrderId()));
        List<OrderItemDto> orderItemDtos = new ArrayList<>();
        for (OrderItem orderItem : orderItems) {
            OrderItemDto orderItemDto = BeanUtil.toBean(orderItem, OrderItemDto.class);
            SkuOrderItem skuItem = skuOrderItemService.getOne(new QueryWrapper<SkuOrderItem>().eq("order_item_id", orderItem.getOrderItemId()));
            if(skuItem!=null) {
                orderItemDto.setSkuId(skuItem.getSkuId());
                orderItemDto.setSkuCount(skuItem.getSkuCount());
                orderItemDto.setSkuName(skuItem.getSkuName());
                orderItemDto.setSkuOrderItemId(skuItem.getSkuOrderItemId());
                orderItemDto.setDeliveryStatus(skuItem.getDeliveryStatus());
                orderItemDto.setUserId(order.getUserId());
                orderDetailDto.setUserName(userDetailDto.getUserName());
            }
            ServiceOrderItem serviceOrderItem = serviceOrderItemService.getOne(new QueryWrapper<ServiceOrderItem>().eq("order_item_id", orderItem.getOrderItemId()));
            if(serviceOrderItem!=null){
                orderItemDto.setServiceOrderItemId(serviceOrderItem.getServiceOrderItemId());
                orderItemDto.setServiceId(serviceOrderItem.getServiceId());
                orderItemDto.setStoreId(serviceOrderItem.getStoreId());
                orderItemDto.setServiceName(serviceOrderItem.getServiceName());
                orderItemDto.setServiceStatus(serviceOrderItem.getServiceStatus());
                orderItemDto.setUserId(order.getUserId());
                orderDetailDto.setUserName(userDetailDto.getUserName());
            }
            orderItemDtos.add(orderItemDto);

        }
        orderDetailDto.setOrderItemDtoList(orderItemDtos);
        if(orderDetailDto.getOrderStatus()==0){
            orderDetailDto.setOrderStatusName("待支付");
        }
        if(orderDetailDto.getOrderStatus()==1){
            orderDetailDto.setOrderStatusName("已支付");
        }
        if(orderDetailDto.getOrderStatus()==2){
            orderDetailDto.setOrderStatusName("已取消");
        }
        if(orderDetailDto.getOrderStatus()==3){
            orderDetailDto.setOrderStatusName("支付中");
        }
        if(orderDetailDto.getOrderStatus()==4){
            orderDetailDto.setOrderStatusName("已完成");
        }
        return Result.success(orderDetailDto);
    }

    @Override
    public Result<OrderDetailDto> getOrderForStore(Long storeId) {

        return null;
    }

    @Override
    public Result<OrderItemDto> getOrderItemDetail(Long orderItemId) {
        OrderItem byId = orderItemService.getById(orderItemId);
        OrderItemDto orderItemDto = BeanUtil.copyProperties(byId, OrderItemDto.class);
        /**
         * 条目类型： 0.商品条目 1.服务条目
         */
        if (byId.getTypeFlag()==0){
            SkuOrderItem skuOrderItem = skuOrderItemService.
                    getOne(new QueryWrapper<SkuOrderItem>().eq("order_item_id", byId.getOrderItemId()));
            BeanUtil.copyProperties(skuOrderItem,orderItemDto);
        }else {
            ServiceOrderItem serviceOrderItem = serviceOrderItemService.
                    getOne(new QueryWrapper<ServiceOrderItem>().eq("order_item_id", byId.getOrderItemId()));
            BeanUtil.copyProperties(serviceOrderItem,orderItemDto);
        }
        return Result.success(orderItemDto);
    }

    @Override
    public Result<List<OrderItemDto>> getStoreOrder(Long storeId) {
        ArrayList<OrderItemDto> orderItemDtos = new ArrayList<>();
        List<ServiceOrderItem> items = serviceOrderItemService.list(new QueryWrapper<ServiceOrderItem>()
                .eq("store_id", storeId));
        for (ServiceOrderItem item : items) {
            OrderItem orderItem = orderItemService.getById(item.getOrderItemId());
            OrderItemDto orderItemDto = BeanUtil.copyProperties(orderItem, OrderItemDto.class);
            Order order = orderService.getById(orderItem.getOrderId());
            orderItemDto.setUserId(order.getUserId());
            orderItemDto.setUserName(order.getUserName());
//            if (orderItem.getTypeFlag()==0){
//                SkuOrderItem skuOrderItem = skuOrderItemService.
//                        getOne(new QueryWrapper<SkuOrderItem>().eq("order_item_id", item.getOrderItemId()));
//                BeanUtil.copyProperties(skuOrderItem,orderItemDto);
//            }else {
//                ServiceOrderItem serviceOrderItem = serviceOrderItemService.
//                        getOne(new QueryWrapper<ServiceOrderItem>().eq("order_item_id", item.getOrderItemId()));
                BeanUtil.copyProperties(item,orderItemDto);
//            }
            orderItemDtos.add(orderItemDto);
        }
        return Result.success(orderItemDtos);
    }
}
